package com.cn.wanxi.util;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Tool {
    private Tool() {
    }

    /**
     * 将字符串转换为整型
     *
     * @param value
     * @return
     */
    public static int stringToInt(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {

        }
        return 0;
    }

    public static int stringToEnable(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {

        }
        return -1;
    }

    public static int stringToPageNum(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {

        }
        return 1;
    }

    public static int stringToPageSize(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {

        }
        return 10;
    }

    public static String encodePassword(String password) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            //加密后的字符串
            return base64en.encode(messageDigest.digest(password.getBytes("utf-8")));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String nullToString(String value) {

        return value == null ? "" : value;

    }
}
